package com.prs.crm.action.cqunicom;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.Util;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.annotation.Clew;
import com.prs.crm.context.AppContext;
import com.prs.crm.domain.cqunicom.HighGradeExpected;
import com.prs.crm.domain.cqunicom.LogSerch;
import com.prs.crm.domain.cqunicom.MidBusinessOrder;
import com.prs.crm.domain.cqunicom.NoteEditor;
import com.prs.crm.domain.cqunicom.PoplarOrder;
import com.prs.crm.domain.cqunicom.Services;
import com.prs.crm.domain.cqunicom.ServicesType;
import com.prs.crm.service.cqunicom.HighGradeExpectedService;
import com.prs.crm.service.cqunicom.LogSerchService;
import com.prs.crm.service.cqunicom.MidBusinessOrderService;
import com.prs.crm.service.cqunicom.NoteEditorService;
import com.prs.crm.service.cqunicom.OrderService;
import com.prs.crm.service.cqunicom.ServicesService;
import com.prs.crm.service.cqunicom.ServicesTypeService;
import com.prs.crm.service.sys.LogService;

/**
 * @author  WangTing
 *
 * 创建时间：2011-3-4 
 * 
 */
public class ServicesAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	private LogSerch logSerch;
	private LogSerchService logSerchService;
	private HttpServletRequest servletRequest;
	private Services services;
	private ServicesService servicesService;
	private ServicesType servicesType;
	private ServicesTypeService servicesTypeService;
	private List<ServicesType> types;
	private Integer servicesIds=0;
	private MidBusinessOrderService midBusinessOrderService;
	private NoteEditorService NoteEditorService;
	private HighGradeExpectedService  HighGradeExpectedService;
	private OrderService orderService;
	private PaginationSupport pagination;
	private LogService logService;
	
	@Action("editServices")
	public String edit() {
		this.types = this.getServicesTypeService().getAllServicesType();
		
		return SUCCESS;
	}
	
	@Action("viewServices")
	public String view() {
		return SUCCESS;
	}
	
	@Action("listServices")
	public String listServices() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Services.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getServicesService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	

	@Clew(results = { "success", "none" })
	@Action(value = "saveServices", results = {
			@Result(name = "success", type = "redirect", location = "editServices"),
			@Result(name = "none", type = "redirect", location = "editServices"),
			@Result(name = "error", type = "chain", location = "editServices") })
	public String save() {
		LogSerch  log=new LogSerch();
		log.setCandlePeron(AppContext.getAccountContext().currentUser().getUserName());
	
		log.setCandleContents("添加了'"+services.getName()+"'服务名称");
		this.getLogSerchService().save(log);
	
		   services.setNoteHand("1");
		   this.getServicesService().save(services);
		   return SUCCESS;
	}

	@Action(value = "removeServices", results = {
			@Result(name = "success", type = "redirect", location = "listServices"),
			@Result(name = "error", type = "chain", location = "listServices") })
	public String remove() {
		if(servicesIds==0){
			this.setPrompt("请选择你要删除的服务名称!");
			return ERROR;
		}else{
			Services so=this.getServicesService().get(servicesIds);
			List<MidBusinessOrder>  miList=this.getMidBusinessOrderService().getMidBusinessOrder(so);
			List<NoteEditor> editList=this.getNoteEditorService().getByServicesNoteEditor(so);
			List<HighGradeExpected> hightList=this.getHighGradeExpectedService().getByServicesHighGradeExpected(so);
			List<PoplarOrder> PopList=this.getOrderService().getByServicesOrder(so);
			int counts=0;
			if(miList.size()>0 || editList.size()>0 || hightList.size()>0 || PopList.size()>0){
				counts++;
			}
			if(counts>0){
				this.setPrompt("该服务正在使用中不能删除!");
				return ERROR;	
			}else{
				LogSerch  log=new LogSerch();
				log.setCandlePeron(AppContext.getAccountContext().currentUser().getUserName());
				
				log.setCandleContents("删除了'"+so.getName()+"'服务名称");
				this.getLogSerchService().save(log);
				this.getServicesService().remove(so);
				this.setPrompt("操作成功");
				return SUCCESS;
			}
		}
	}
	
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public Services getServices() {
		return services;
	}

	public void setServices(Services services) {
		this.services = services;
	}

	public ServicesService getServicesService() {
		return servicesService;
	}

	public void setServicesService(ServicesService servicesService) {
		this.servicesService = servicesService;
	}

	public ServicesType getServicesType() {
		return servicesType;
	}

	public void setServicesType(ServicesType servicesType) {
		this.servicesType = servicesType;
	}

	public ServicesTypeService getServicesTypeService() {
		return servicesTypeService;
	}

	public void setServicesTypeService(ServicesTypeService servicesTypeService) {
		this.servicesTypeService = servicesTypeService;
	}

	


	public Integer getServicesIds() {
		return servicesIds;
	}

	public void setServicesIds(Integer servicesIds) {
		this.servicesIds = servicesIds;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public static long getSerialVersionUID() {
		return serialVersionUID;
	}

	public List<ServicesType> getTypes() {
		return types;
	}

	public void setTypes(List<ServicesType> types) {
		this.types = types;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}
	public LogService getLogService() {
		return logService;
	}
	public void setLogService(LogService logService) {
		this.logService = logService;
	}

	public MidBusinessOrderService getMidBusinessOrderService() {
		return midBusinessOrderService;
	}

	public void setMidBusinessOrderService(
			MidBusinessOrderService midBusinessOrderService) {
		this.midBusinessOrderService = midBusinessOrderService;
	}

	public NoteEditorService getNoteEditorService() {
		return NoteEditorService;
	}

	public void setNoteEditorService(NoteEditorService noteEditorService) {
		NoteEditorService = noteEditorService;
	}

	public HighGradeExpectedService getHighGradeExpectedService() {
		return HighGradeExpectedService;
	}

	public void setHighGradeExpectedService(
			HighGradeExpectedService highGradeExpectedService) {
		HighGradeExpectedService = highGradeExpectedService;
	}

	public OrderService getOrderService() {
		return orderService;
	}

	public void setOrderService(OrderService orderService) {
		this.orderService = orderService;
	}

	public LogSerch getLogSerch() {
		return logSerch;
	}

	public void setLogSerch(LogSerch logSerch) {
		this.logSerch = logSerch;
	}

	public LogSerchService getLogSerchService() {
		return logSerchService;
	}

	public void setLogSerchService(LogSerchService logSerchService) {
		this.logSerchService = logSerchService;
	}

	public HttpServletRequest getServletRequest() {
		return servletRequest;
	}

	public void setServletRequest(HttpServletRequest servletRequest) {
		this.servletRequest = servletRequest;
	}


}
